<?php


namespace app\common\service\companyIndustry\realtime;

use app\common\service\BaseService;
use app\common\service\ControlService;
use app\common\service\FormulaService;
use app\common\service\MongoService;

/**
 * 脱硫排放量
 */
class SulfurEmissionService extends BaseService
{
    /**
     * +----------------------------------------------------------
     * 7.3 单个公司 - 关于脱硫 - 排放量
     * 获取实时企业 - 排口 - 脱硫小时平均排放量 - 平均值(默认前七天)
     * +----------------------------------------------------------
     * @param $companyId
     * @param $endDay
     * @param $daysNumber
     * @param $emissionUnit
     * @param bool $everyDay 是否输出每天
     * +----------------------------------------------------------
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-07-03
     * +----------------------------------------------------------
     */
    public function getRealtimeSingleCompanyEmissionSulfurMinToHourAvg($companyId, $endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 'kg', $everyDay = false)
    {
        $formula = new FormulaService();
        $mongo   = new MongoService();
        //step1:通过条件获取脱硫数据
        if ($endDay == 'yesterday') {
            $startDay = date('Y-m-d', strtotime("-" . $daysNumber . " day")) . ' 00:00:00';
            $endDay   = date('Y-m-d', strtotime("-1 day")) . ' 23:59:59';
        } else {
            $startDay = date('Y-m-d', strtotime($endDay) - ($daysNumber - 1) * 3600 * 24) . ' 00:00:00';
            $endDay   = date('Y-m-d', strtotime($endDay)) . ' 23:59:59';
        }

        $wholeHours  = $daysNumber * 24;  //全部小时
        $denominator = 24 * $daysNumber * $formula->getUnit($emissionUnit);  //分母 = 24小时 * 天数 * 单位
        $table       = 'company_industry_monitor_sulfur_minute';
        $data        = $mongo->getSulfurDataByMongoSql($startDay, $endDay, $table, $denominator, false, (int)$companyId, $everyDay);

        //step2:丢失小时数
        $lostHours = $mongo->lostHours($startDay, $endDay, $table, $wholeHours);

        //step3:转为数组并取到有用的值
        foreach ($data as $k => $v) {
            if ($everyDay) {
                $data[$k]['publish_time'] = $v['_id']->publish_time;
            }
            $data[$k]['company_id']               = $v['_id']->company_id;
            $data[$k]['company_name']             = $v['_id']->company_name;
            $data[$k]['so2_emissions']            = $v['so2_emissions'];   //原数据
            $data[$k]['so2_emissions_supplement'] = $formula->supplementData($lostHours, $wholeHours, $v['so2_emissions']);   //补数后的数据
            $data[$k]['tsp_emissions']            = $v['tsp_emissions'];
            $data[$k]['tsp_emissions_supplement'] = $formula->supplementData($lostHours, $wholeHours, $v['tsp_emissions']);
            unset($data[$k]['_id']);
        }
        //step4:返回
        return $data;
    }
    // +----------------------------------------------------------------------
    // | （一）设施
    // +----------------------------------------------------------------------
    /**
     * （减排前）单个设施 - 脱硫排放量 - 总量
     * @param integer $facilityId 设施ID
     * @param string $planNumber 预案编号
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-07-24
     */
    public function beforeSingleCompanySingleDrainSingleFacilityTotal($facilityId, $planNumber, $every = false)
    {
        $mongo    = new MongoService();
        $control  = new ControlService();
        $startDay = $control->getBeforeStartTime($planNumber);
        $endDay   = $control->getBeforeEndTime($planNumber);
        $result   = $mongo->getSulfurFieldOfSumOfFacility($startDay, $endDay, $facilityId, '$emissions', $every);
        if ($every) {
            $data = [];
            foreach ($result as $k => $v) {
                $data[$k]['facility_id']  = $v['_id']->facility_id;
                $data[$k]['publish_time'] = $v['_id']->publish_time;
                $data[$k]['emissions']    = $v['field'];
            }
            return $data;
        } else {
            //K值数
            $kBeforeValue = $mongo->kBeforeValue();
            //填数后的值
            $total = round($result * $kBeforeValue, 2);
            $total = isset($total) ? $total : 0;
            return $total;
        }
    }

    /**
     * （减排前）单个设施 - 脱硫排放量 - 小时均值
     * @param $facilityId
     * @param $planNumber
     * @return float
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-07-24
     */
    public function beforeSingleCompanySingleDrainSingleFacilityHourAverage($facilityId, $planNumber)
    {
        $total      = $this->beforeSingleCompanySingleDrainSingleFacilityTotal($facilityId, $planNumber);
        $control    = new ControlService();
        $totalHours = $control->getBeforeHours();
        $average    = round($total / $totalHours, 2);
        $average    = isset($average) ? $average : 0;
        return $average;
    }

    /**
     * （减排前）单个设施 - 脱硫排放量 - 天均值
     * @param $facilityId
     * @param $planNumber
     * @return float
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-07-24
     */
    public function beforeSingleCompanySingleDrainSingleFacilityDayAverage($facilityId, $planNumber, $every = false)
    {
        $total = $this->beforeSingleCompanySingleDrainSingleFacilityTotal($facilityId, $planNumber, $every);
        if ($every) {
            return $total;
        } else {
            $control    = new ControlService();
            $totalHours = $control->getBeforeDays();
            $average    = round($total / $totalHours, 2);
            $average    = isset($average) ? $average : 0;
            return $average;
        }

    }

    // +----------------------------------------------------------------------
    // | （二）排口 / 总量，小时均值，天均值
    // +----------------------------------------------------------------------
    /**
     * （减排前）单个排口 - 脱硫排放量 - 总量
     * @param integer $drainId 排口ID
     * @param string $planNumber 预案编号
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-07-24
     */
    public function beforeSingleCompanySingleDrainTotal($drainId, $planNumber)
    {
        $mongo    = new MongoService();
        $control  = new ControlService();
        $startDay = $control->getBeforeStartTime($planNumber);
        $endDay   = $control->getBeforeEndTime($planNumber);
        $result   = $mongo->getSulfurFieldOfSumOfDrain($startDay, $endDay, $drainId, '$emissions');
        //K值数
        $kBeforeValue = $mongo->kBeforeValue();
        //填数后的值
        $total = round($result * $kBeforeValue, 2);
        $total = isset($total) ? $total : 0;
        return $total;
    }

    /**
     * （减排前）单个排口 - 脱硫排放量 - 小时均值
     * @param $drainId
     * @param $planNumber
     * @return float
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-07-24
     */
    public function beforeSingleCompanySingleDrainHourAverage($drainId, $planNumber)
    {
        $total      = $this->beforeSingleCompanySingleDrainTotal($drainId, $planNumber);
        $control    = new ControlService();
        $totalHours = $control->getBeforeHours();
        $average    = round($total / $totalHours, 2);
        $average    = isset($average) ? $average : 0;
        return $average;
    }

    /**
     * （减排前）单个排口 - 脱硫排放量 - 天均值
     * @param $drainId
     * @param $planNumber
     * @return float
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-07-24
     */
    public function beforeSingleCompanySingleDrainDayAverage($drainId, $planNumber)
    {
        $total      = $this->beforeSingleCompanySingleDrainTotal($drainId, $planNumber);
        $control    = new ControlService();
        $totalHours = $control->getBeforeDays();
        $average    = round($total / $totalHours, 2);
        $average    = isset($average) ? $average : 0;
        return $average;
    }

    // +----------------------------------------------------------------------
    // | （三）排口 小时具体
    // +----------------------------------------------------------------------

    /**
     * （减排前）脱硫排口总和 - 每小时
     * @param $drainId
     * @param $planNumber
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function beforeSingleCompanySingleDrainEveryHour($drainId, $planNumber)
    {
        $mongo    = new MongoService();
        $control  = new ControlService();
        $startDay = $control->getBeforeStartTime($planNumber);
        $endDay   = $control->getBeforeEndTime($planNumber);
        $result   = $mongo->getSulfurFieldOfSumOfDrain($startDay, $endDay, $drainId, '$emissions', 'hour'); //小时
        $newData  = [];
        foreach ($result as $k => $v) {
            $newData[$k]['drain_id']     = $v['_id']->drain_id;
            $newData[$k]['time']         = $v['_id']->publish_time;
            $newData[$k]['so2_emission'] = $v['field'];
        }
        return $newData;
    }

    /**
     * （减排前）脱硫排口总和 - 每小时
     * @param $drainId
     * @param $planNumber
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function beforeSingleCompanySingleDrainHourAvg($drainId, $planNumber)
    {
        $mongo    = new MongoService();
        $control  = new ControlService();
        $startDay = $control->getBeforeStartTime($planNumber);
        $endDay   = $control->getBeforeEndTime($planNumber);
        $minutes  = $control->getControlDoingMinutes($planNumber);
        $result   = $mongo->getSulfurFieldOfSumOfDrain($startDay, $endDay, $drainId, '$emissions');
        if (!$result) {
            $result = 0;
        }
        $result = $result / $minutes * 60;
        return $result;
    }

    /**
     * （管控中）脱硫排口总和 - 每小时
     * @param $drainId
     * @param $planNumber
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function controlSingleCompanySingleDrainEveryHour($drainId, $planNumber)
    {
        $mongo    = new MongoService();
        $control  = new ControlService();
        $startDay = $control->getControlStartTime($planNumber);
        $endDay   = $control->getControlEndTime($planNumber);
        $result   = $mongo->getSulfurFieldOfSumOfDrain($startDay, $endDay, $drainId, '$emissions', 'hour'); //小时
        $newData  = [];
        foreach ($result as $k => $v) {
            $newData[$k]['drain_id']     = $v['_id']->drain_id;
            $newData[$k]['time']         = $v['_id']->publish_time;
            $newData[$k]['so2_emission'] = $v['field'];
        }
        return $newData;
    }

    /**
     * （管控中）脱硫排口总和 - 每小时
     * @param $drainId
     * @param $planNumber
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function controlSingleCompanySingleDrainHourAvg($drainId, $planNumber)
    {
        $mongo       = new MongoService();
        $control     = new ControlService();
        $startDay    = $control->getControlStartTime($planNumber);
        $endDoingDay = $control->getControlDoingEndTime();
        if (strtotime($endDoingDay) < strtotime($startDay)) {
            $result = 0;
        } else {
            $minutes = $control->getControlDoingMinutes($planNumber);
            $result  = $mongo->getSulfurFieldOfSumOfDrain($startDay, $endDoingDay, $drainId, '$emissions');
            if (!$result) {
                $result = 0;
            }
            $result = $result / $minutes * 60;
        }
        return $result;
    }

    // +----------------------------------------------------------------------
    // | （四）排口 天具体
    // +----------------------------------------------------------------------

    /**
     * （减排前）脱硫排口总和 - 每天
     * @param $drainId
     * @param $planNumber
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function beforeSingleCompanySingleDrainEveryDay($drainId, $planNumber)
    {
        $mongo    = new MongoService();
        $control  = new ControlService();
        $startDay = $control->getBeforeStartTime($planNumber) . ' 00:00:00';
        $endDay   = $control->getBeforeEndTime($planNumber) . ' 23:59:59';
        $result   = $mongo->getSulfurFieldOfSumOfDrain($startDay, $endDay, $drainId, '$emissions', 'day'); //小时
        $newData  = [];
        foreach ($result as $k => $v) {
            $newData[$k]['drain_id']     = $v['_id']->drain_id;
            $newData[$k]['time']         = $v['_id']->publish_time;
            $newData[$k]['so2_emission'] = $v['field'];
        }
        return $newData;
    }

    /**
     * （管控中）脱硫排口总和 - 每天
     * @param $drainId
     * @param $planNumber
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function controlSingleCompanySingleDrainEveryDay($drainId, $planNumber)
    {
        $mongo    = new MongoService();
        $control  = new ControlService();
        $startDay = $control->getControlStartTime($planNumber) . ' 00:00:00';
        $endDay   = $control->getControlEndTime($planNumber) . ' 23:59:59';
        $result   = $mongo->getSulfurFieldOfSumOfDrain($startDay, $endDay, $drainId, '$emissions', 'day'); //小时
        $newData  = [];
        foreach ($result as $k => $v) {
            $newData[$k]['drain_id']     = $v['_id']->drain_id;
            $newData[$k]['time']         = $v['_id']->publish_time;
            $newData[$k]['so2_emission'] = $v['field'];
        }
        return $newData;
    }

    /**
     * （减排前）单个排口 - 脱硫排放量 - 小时均值
     * @param $drainId
     * @param $planNumber
     * @return float
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-07-24
     */
    public function beforeSingleCompanySingleDrainHourDetails($drainId, $planNumber)
    {
        $total      = $this->beforeSingleCompanySingleDrainTotal($drainId, $planNumber);
        $control    = new ControlService();
        $totalHours = $control->getBeforeHours();
        $average    = round($total / $totalHours, 2);
        $average    = isset($average) ? $average : 0;
        return $average;
    }
    // +----------------------------------------------------------------------
    // | 公司
    // +----------------------------------------------------------------------
    /**
     * （减排前）公司 - 脱硫排放量 - 总量
     * @param integer $companyId 公司ID
     * @param string $planNumber 预案编号
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-07-24
     */
    public function beforeSingleCompanyTotal($companyId, $planNumber)
    {
        $mongo    = new MongoService();
        $control  = new ControlService();
        $startDay = $control->getBeforeStartTime($planNumber);
        $endDay   = $control->getBeforeEndTime($planNumber);
        $result   = $mongo->getSulfurFieldOfSumOfCompany($startDay, $endDay, $companyId, '$emissions');
        //K值数
        $kBeforeValue = $mongo->kBeforeValue();
        //填数后的值
        $total = round($result * $kBeforeValue, 2);
        $total = isset($total) ? $total : 0;
        return $total;
    }
    // +----------------------------------------------------------------------
    // | 行业
    // +----------------------------------------------------------------------
    /**
     * （减排前）行业 - 脱硫排放量 - 总量
     * @param integer $industryId 行业ID
     * @param string $planNumber 预案编号
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-07-24
     */
    public function beforeSingleIndustryTotal($industryId, $planNumber)
    {
        $mongo    = new MongoService();
        $control  = new ControlService();
        $startDay = $control->getBeforeStartTime($planNumber);
        $endDay   = $control->getBeforeEndTime($planNumber);
        $result   = $mongo->getSulfurFieldOfSumOfIndustry($startDay, $endDay, $industryId, '$emissions');
        //K值数
        $kBeforeValue = $mongo->kBeforeValue();
        //填数后的值
        $total = round($result * $kBeforeValue, 2);
        $total = isset($total) ? $total : 0;
        return $total;
    }

    /**
     * （减排前）行业 - 脱硫排放量 - 总量
     * @param integer $industryId 行业ID
     * @param string $planNumber 预案编号
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-07-24
     */
    public function beforeIndustryEvery($planNumber)
    {
        $mongo    = new MongoService();
        $control  = new ControlService();
        $startDay = $control->getBeforeStartTime($planNumber);
        $endDay   = $control->getBeforeEndTime($planNumber);
        $result   = $mongo->getSulfurFieldOfSumOfEveryIndustry($startDay, $endDay, '$emissions');
        //K值数
        $kBeforeValue = $mongo->kBeforeValue();
        //填数后的值
        $newData = [];
        foreach ($result as $k => $v) {
            $newData[$k]['industry_id']   = $v['industry_id'];
            $newData[$k]['so2_emissions'] = round($v['so2_emissions'] * $kBeforeValue, 2);
        }
        return $newData;
    }

    // +----------------------------------------------------------------------
    // | 之前的
    // +----------------------------------------------------------------------

    /**
     * +----------------------------------------------------------
     * 7.1 所有公司 - 所有排口 - 关于脱硫 - 排放量
     * 获取实时企业 - 排口 - 脱硫小时平均排放量 - 平均值(默认前七天)
     * +----------------------------------------------------------
     * @param $endDay
     * @param $daysNumber
     * @param $emissionUnit
     * @param $everyDay
     * +----------------------------------------------------------
     * @@return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getRealtimeAllCompanyDrainEmissionSulfurMinToHourAvg($endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 'kg', $everyDay = false)
    {
        //step1:通过条件获取脱硫数据
        if ($endDay == 'yesterday') {
            $startDay = date('Y-m-d', strtotime("-" . $daysNumber . " day")) . ' 00:00:00';
            $endDay   = date('Y-m-d', strtotime("-1 day")) . ' 23:59:59';
        } else {
            $startDay = date('Y-m-d', strtotime($endDay) - ($daysNumber - 1) * 3600 * 24) . ' 00:00:00';
            $endDay   = date('Y-m-d', strtotime($endDay)) . ' 23:59:59';
        }

        $wholeHours  = $daysNumber * 24;  //全部小时
        $formula     = new FormulaService();
        $denominator = 24 * $daysNumber * $formula->getUnit($emissionUnit);  //分母 = 24小时 * 天数 * 单位
        $table       = 'company_industry_monitor_sulfur_minute';
        $mongo       = new MongoService();
        $data        = $mongo->getSulfurDataByMongoSql($startDay, $endDay, $table, $denominator, true, $everyDay);

        //step2:丢失小时数
        $lostHours = $mongo->lostHours($startDay, $endDay, $table, $wholeHours);

        $formula = new FormulaService();

        //step3:转为数组并取到有用的值
        foreach ($data as $k => $v) {
            $data[$k]['drain_id']                 = $v['_id']->drain_id;
            $data[$k]['drain_name']               = $v['_id']->drain_name;
            $data[$k]['company_id']               = $v['_id']->company_id;
            $data[$k]['company_name']             = $v['_id']->company_name;
            $data[$k]['so2_emissions']            = $v['so2_emissions'];   //原数据
            $data[$k]['so2_emissions_supplement'] = $formula->supplementData($lostHours, $wholeHours, $v['so2_emissions']);   //补数后的数据
            $data[$k]['tsp_emissions']            = $v['tsp_emissions'];
            $data[$k]['tsp_emissions_supplement'] = $formula->supplementData($lostHours, $wholeHours, $v['tsp_emissions']);
            unset($data[$k]['_id']);
        }
        $data = field_as_key($data, 'company_id');
        //step4:返回
        return $data;
    }

    /**
     * +----------------------------------------------------------
     * 7.1 所有公司 - 所有排口 - 关于脱硫 - 排放量
     * 获取实时企业 - 排口 - 脱硫小时平均排放量 - 平均值(默认前七天)
     * +----------------------------------------------------------
     * @param $companyId
     * @param $endDay
     * @param $emissionUnit
     * @param $daysNumber
     * +----------------------------------------------------------
     * @@return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getRealtimeSingleCompanyDrainEmissionSulfurMinToHourAvg($companyId, $endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 'kg')
    {
        //step1:通过条件获取脱硫数据
        if ($endDay == 'yesterday') {
            $startDay = date('Y-m-d', strtotime("-" . $daysNumber . " day")) . ' 00:00:00';
            $endDay   = date('Y-m-d', strtotime("-1 day")) . ' 23:59:59';
        } else {
            $startDay = date('Y-m-d', strtotime($endDay) - ($daysNumber - 1) * 3600 * 24) . ' 00:00:00';
            $endDay   = date('Y-m-d', strtotime($endDay)) . ' 23:59:59';
        }

        $wholeHours     = $daysNumber * 24;  //全部小时
        $formulaService = new FormulaService();
        $denominator    = 24 * $daysNumber * $formulaService->getUnit($emissionUnit);  //分母 = 24小时 * 天数 * 单位
        $table          = 'company_industry_monitor_sulfur_minute';
        $mongoService   = new MongoService();
        $data           = $mongoService->getSulfurDataByMongoSql($startDay, $endDay, $table, $denominator, true, (int)$companyId);

        //step2:丢失小时数
        $lostHours = $mongoService->lostHours($startDay, $endDay, $table, $wholeHours);
        $formula   = new FormulaService();
        //step3:转为数组并取到有用的值
        foreach ($data as $k => $v) {
            $data[$k]['drain_id']                 = $v['_id']->drain_id;
            $data[$k]['drain_name']               = $v['_id']->drain_name;
            $data[$k]['company_id']               = $v['_id']->company_id;
            $data[$k]['company_name']             = $v['_id']->company_name;
            $data[$k]['so2_emissions']            = $v['so2_emissions'];   //原数据
            $data[$k]['so2_emissions_supplement'] = $formula->supplementData($lostHours, $wholeHours, $v['so2_emissions']);   //补数后的数据
            $data[$k]['tsp_emissions']            = $v['tsp_emissions'];
            $data[$k]['tsp_emissions_supplement'] = $formula->supplementData($lostHours, $wholeHours, $v['tsp_emissions']);
            unset($data[$k]['_id']);
        }
        //step4:返回
        return $data;
    }

    /**
     * +----------------------------------------------------------
     * 7.2 所有公司 - 关于脱硫 - 排放量
     * 获取实时企业 - 排口 - 脱硫小时平均排放量 - 平均值(默认前七天)
     * +----------------------------------------------------------
     * @param $endDay
     * @param $daysNumber
     * @param $emissionUnit
     * @param $industryId
     * @param $everyDay
     * +----------------------------------------------------------
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getRealtimeAllCompanyEmissionSulfurMinToHourAvg($endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 'kg', $industryId = false, $everyDay = false)
    {
        //step1:通过条件获取脱硫数据
        if ($endDay == 'yesterday') {
            $startDay = date('Y-m-d', strtotime("-" . $daysNumber . " day")) . ' 00:00:00';
            $endDay   = date('Y-m-d', strtotime("-1 day")) . ' 23:59:59';
        } else {
            $startDay = date('Y-m-d', strtotime($endDay) - ($daysNumber - 1) * 3600 * 24) . ' 00:00:00';
            $endDay   = date('Y-m-d', strtotime($endDay)) . ' 23:59:59';
        }

        $wholeHours   = $daysNumber * 24;  //全部小时
        $formula      = new FormulaService();
        $denominator  = 24 * $daysNumber * $formula->getUnit($emissionUnit);  //分母 = 24小时 * 天数 * 单位
        $table        = 'company_industry_monitor_sulfur_minute';
        $mongoService = new MongoService();
        if ($industryId && $everyDay == true) {
            $data = $mongoService->getSulfurDataByMongoSql($startDay, $endDay, $table, $denominator, false, false, true, $industryId);
        } else if ($industryId && $everyDay == false) {
            $data = $mongoService->getSulfurDataByMongoSql($startDay, $endDay, $table, $denominator, false, false, false, $industryId);
        } else {
            $data = $mongoService->getSulfurDataByMongoSql($startDay, $endDay, $table, $denominator, false);
        }
        //step2:丢失小时数
        $lostHours = $mongoService->lostHours($startDay, $endDay, $table, $wholeHours);
        $formula   = new FormulaService();
        //step3:转为数组并取到有用的值
        if ($industryId && $everyDay == true) {
            foreach ($data as $k => $v) {
                $data[$k]['industry_id']              = $v['_id']->industry_id;
                $data[$k]['publish_time']             = $v['_id']->publish_time;
                $data[$k]['so2_emissions']            = $v['so2_emissions'];   //原数据
                $data[$k]['so2_emissions_supplement'] = $formula->supplementData($lostHours, $wholeHours, $v['so2_emissions']);   //补数后的数据
                $data[$k]['tsp_emissions']            = $v['tsp_emissions'];
                $data[$k]['tsp_emissions_supplement'] = $formula->supplementData($lostHours, $wholeHours, $v['tsp_emissions']);
                unset($data[$k]['_id']);
            }
        } else if ($industryId && $everyDay == false) {
            foreach ($data as $k => $v) {
                $data[$k]['industry_id']              = $v['_id']->industry_id;
                $data[$k]['so2_emissions']            = $v['so2_emissions'];   //原数据
                $data[$k]['so2_emissions_supplement'] = $formula->supplementData($lostHours, $wholeHours, $v['so2_emissions']);   //补数后的数据
                $data[$k]['tsp_emissions']            = $v['tsp_emissions'];
                $data[$k]['tsp_emissions_supplement'] = $formula->supplementData($lostHours, $wholeHours, $v['tsp_emissions']);
                unset($data[$k]['_id']);
            }
        } else {
            foreach ($data as $k => $v) {
                $data[$k]['company_id']               = $v['_id']->company_id;
                $data[$k]['company_name']             = $v['_id']->company_name;
                $data[$k]['so2_emissions']            = $v['so2_emissions'];   //原数据
                $data[$k]['so2_emissions_supplement'] = $formula->supplementData($lostHours, $wholeHours, $v['so2_emissions']);   //补数后的数据
                $data[$k]['tsp_emissions']            = $v['tsp_emissions'];
                $data[$k]['tsp_emissions_supplement'] = $formula->supplementData($lostHours, $wholeHours, $v['tsp_emissions']);
                unset($data[$k]['_id']);
            }
            $data = field_as_key($data, 'company_id');
        }

        //step4:返回
        return $data;
    }
}